package cn.sz.dzy.rest.controller;

import cn.hutool.core.io.resource.InputStreamResource;
import cn.sz.dzy.rest.dto.UserDto;
import cn.sz.dzy.rest.dto.UserTestDto;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.*;

/**
 * @program: RestTemplate
 * @description:
 * @author: DingZhenYun
 * @create: 2024-04-18 15:51
 **/
@RestController
@RequestMapping("/up")
public class UpstreamController {

    @GetMapping("/get")
    public UserDto get() {
        return new UserDto(1, "大家好，我是ikun",22);
    }

    @GetMapping("/get/{id}/{name}")
    public UserDto get(@PathVariable("id") Integer id, @PathVariable("name") String name) {
        return new UserDto(1, "大家好，我是ikun",22);
    }

    @GetMapping("/geByParam")
    public UserDto geByParam(@RequestParam("id") Integer id, @RequestParam("name") String name) {
        return new UserDto(1, "大家好，我是ikun",22);
    }

    @GetMapping("/getList")
    public List getList() {
        return Arrays.asList(
                new UserDto(1, "大家好，我是ikun",22),
                new UserDto(2, "大家好，我是GGB",22)
        );
    }


    /**
     * 下载文件
     *
     * @return
     */
    @GetMapping("/downFile")
    @ResponseBody
    public HttpEntity downFile() {
        //将文件流封装为InputStreamResource对象
        InputStream inputStream= null;
        try {
            inputStream = new FileInputStream(new File("C:\\Users\\dingz\\Desktop\\file\\1.txt"));
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        }
        InputStreamResource inputStreamResource = new InputStreamResource(inputStream);
        //设置header
        MultiValueMap headers = new HttpHeaders();
        headers.add(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=1.txt");
        HttpEntity httpEntity = new HttpEntity<>(inputStreamResource);
        return httpEntity;
    }
    /**
     * 下载文件
     *
     * @return
     */
    @GetMapping("/downBigFile")
    @ResponseBody
    public HttpEntity downBigFile() {
        //将文件流封装为InputStreamResource对象
        InputStream inputStream= null;
        try {
            inputStream = new FileInputStream(new File("C:\\Users\\dingz\\Desktop\\file\\1.txt"));
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        }
        InputStreamResource inputStreamResource = new InputStreamResource(inputStream);
        //设置header
        MultiValueMap headers = new HttpHeaders();
        headers.add(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=1.txt");
         HttpEntity httpEntity = new HttpEntity<>(inputStreamResource);
        return httpEntity;
    }

    @GetMapping("/header")
    public Map<String,List> header(HttpServletRequest request) {
        Map<String,List> header = new LinkedHashMap<>();
        Enumeration headerNames = request.getHeaderNames();
        while (headerNames.hasMoreElements()) {
            String name = (String) headerNames.nextElement();
            Enumeration values = request.getHeaders(name);
            List list = new ArrayList<>();
            while (values.hasMoreElements()) {
                list.add(values.nextElement());
            }
            header.put(name, list);
        }
        return header;
    }

    @GetMapping("/getAll/{path1}/{path2}")
    @ResponseBody
    public Map getAll(@PathVariable("path1") String path1,
                      @PathVariable("path2") String path2,
                      HttpServletRequest request) {
        Map result = new LinkedHashMap<>();
        result.put("path1", path1);
        result.put("path2", path2);
        //头
        Map<String,List> header = new LinkedHashMap<>();
        Enumeration headerNames = request.getHeaderNames();
        while (headerNames.hasMoreElements()) {
            String name = (String) headerNames.nextElement();
            Enumeration values = request.getHeaders(name);
            List list = new ArrayList<>();
            while (values.hasMoreElements()) {
                list.add(values.nextElement());
            }
            header.put(name, list);
        }
        result.put("header", header);
        return result;
    }

    @PostMapping("/form")
    @ResponseBody
    public UserDto form(UserDto userDto) {
        return userDto;
    }

    @PostMapping("/form2")
    public Map form2(@RequestParam("file") MultipartFile file) {
        Map fileMetadata = new LinkedHashMap<>();
        fileMetadata.put("文件名", file.getOriginalFilename());
        fileMetadata.put("文件类型", file.getContentType());
        fileMetadata.put("文件大小(byte)", String.valueOf(file.getSize()));
        return fileMetadata;
    }

    /**
     * 复杂的表单：包含了普通元素、多文件
     *
     * @param userDto
     * @return
     */
    @PostMapping("/form3")
    public Map form3(UserTestDto userDto) {
        Map result = new LinkedHashMap<>();
        result.put("name", userDto.getName());
        result.put("headImg", userDto.getHeadImg().getOriginalFilename());
        result.put("idImgList", Arrays.toString(userDto.getIdImgList().stream().
                map(MultipartFile::getOriginalFilename).toArray()));
        return result;
    }

    /**
     * body中json格式的数据，返回值非泛型
     *
     * @param userDto
     * @return
     */
    @PostMapping("/form4")
    @ResponseBody
    public UserDto form4(@RequestBody UserDto userDto) {
        return userDto;
    }

    /**
     * body中json格式的数据，返回值为泛型
     *
     * @param list
     * @return
     */
    @PostMapping("/form5")
    public List<UserDto> form5(@RequestBody List<UserDto> list) {
        return list;
    }
}
